package cn.z2huo.mybatis.generator.mybatisplus.plugin;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.internal.util.StringUtility;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Properties;

/**
 * <p>类注释和方法注释生成插件
 *
 * @author z2huo
 */
public class DocCommentsPlugin extends PluginAdapter {

    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    /**
     * <p>插件自定义的属性，用于填充文件作者
     */
    private String author;

    @Override
    public void setProperties(Properties properties) {
        super.setProperties(properties);
        this.author = properties.getProperty("author");
    }

    @Override
    public boolean validate(List<String> list) {
        return true;
    }

    /**
     * <p>自定义生成的实体类
     */
    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {

        // 类上面添加注释
        topLevelClass.addJavaDocLine("/**");
        topLevelClass.addJavaDocLine(" * <p>该 DO 对应数据库表为 " + introspectedTable.getFullyQualifiedTable());
        topLevelClass.addJavaDocLine(" * <p>");
        topLevelClass.addJavaDocLine(" * ");
        topLevelClass.addJavaDocLine(" * @author " + author);
        topLevelClass.addJavaDocLine(" * @date   " + this.date2Str(LocalDate.now()));
        topLevelClass.addJavaDocLine(" */");

        return true;
    }

    /**
     * <p>自定义生成的实体类属性
     */
    @Override
    public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
                                       IntrospectedTable introspectedTable, ModelClassType modelClassType) {

        field.addJavaDocLine("/**");
        String remarks = introspectedColumn.getRemarks();
        if (StringUtility.stringHasValue(remarks)) {
            String[] remarkLines = remarks.split(System.lineSeparator());
            for (String remarkLine : remarkLines) {
                field.addJavaDocLine(" * " + remarkLine);
            }
        } else {
            field.addJavaDocLine(" * ");
        }
        field.addJavaDocLine(" */");

        return true;
    }

    /**
     * <p>自定义生成的 DAO 接口
     */
    @Override
    public boolean clientGenerated(Interface interfaze, IntrospectedTable introspectedTable) {

        // 类上面添加注释
        interfaze.addJavaDocLine("/**");
        interfaze.addJavaDocLine(" * <p>该 Mapper 对应数据库表为 " + introspectedTable.getFullyQualifiedTable());
        interfaze.addJavaDocLine(" * <p>");
        interfaze.addJavaDocLine(" * ");
        interfaze.addJavaDocLine(" * @author " + author);
        interfaze.addJavaDocLine(" * @date   " + this.date2Str(LocalDate.now()));
        interfaze.addJavaDocLine(" */");

        return true;
    }

    private String date2Str(LocalDate date) {
        return date.format(DATE_TIME_FORMATTER);
    }

}
